#include "CFootballTeam.h"


CFootballTeam::CFootballTeam(void)
	: m_Score(0)
	, m_Name("Unknown Team")
{
	//initialise players
	CPoint2D Point(0, 0);
	int Index = 0;
	bool Found = false;
	CFootballKeeper* pKeeper = NULL;
	CFootballOutfieldPlayer* pFielder = NULL;

	for(Index = 0; Index < CONST_MAX_PLAYER; Index++) {
		Found = false;
		do{
			Point.SetPoint(GetRandom(0, CFootballField::s_CONST_FIELDLENGTH), GetRandom(0, CFootballField::s_CONST_FIELDWIDTH));
			for(int IndexB = 0; IndexB < Index; IndexB++ ) {
				if(IndexB == 0) {
					pKeeper = (CFootballKeeper*) m_pPlayer[IndexB];
					Found  = pKeeper->GetPosition() == Point;
				} else {
					pFielder = (CFootballOutfieldPlayer*) m_pPlayer[IndexB];
					Found   = pFielder->GetPosition() == Point;
				}
			}
		}while(Found);

		if(Index == 0) {
			m_pPlayer[Index] = new CFootballKeeper(Point.GetX(), Point.GetY());
		} else { 
			m_pPlayer[Index] = new CFootballOutfieldPlayer(Point.GetX(), Point.GetY());
		}
	}
}

CFootballTeam::CFootballTeam(std::string _Name)
	: m_Score(0)
	, m_Name(_Name)
{
	//initialise players
	CPoint2D Point(0, 0);

	bool Found = false;
	CFootballKeeper* pKeeper = NULL;
	CFootballOutfieldPlayer* pFielder = NULL;

	for(int Index = 0; Index < CONST_MAX_PLAYER; Index++) {
		Found = false;
		do{
			Point.SetPoint(GetRandom(0, CFootballField::s_CONST_FIELDLENGTH), GetRandom(0, CFootballField::s_CONST_FIELDWIDTH));
			for(int IndexB = 0; IndexB < Index; IndexB++ ) {
				if(IndexB == 0) {
					pKeeper = (CFootballKeeper*) m_pPlayer[IndexB];
					Found  = pKeeper->GetPosition() == Point;
				} else {
					pFielder = (CFootballOutfieldPlayer*) m_pPlayer[IndexB];
					Found   = pFielder->GetPosition() == Point;
				}
			}
		}while(Found);

		if(Index == 0)
		{
			m_pPlayer[Index] = new CFootballKeeper(Point.GetX(), Point.GetY());
		}
		else
		{
			m_pPlayer[Index] = new CFootballOutfieldPlayer(Point.GetX(), Point.GetY());
		}
	}
}

CFootballTeam::CFootballTeam(CFootballTeam* _Team)
	: m_Score(0)
	, m_Name("Unknown Team 2")
{
	//initialise players
	CPoint2D Point(0, 0);
	int Index = 0;
	bool Found = false;
	CFootballKeeper* pKeeper = NULL;
	CFootballOutfieldPlayer* pFielder = NULL;

	for(Index = 0; Index < CONST_MAX_PLAYER; Index++) {
		Found = false;
		do{
			Point.SetPoint(GetRandom(0, CFootballField::s_CONST_FIELDLENGTH), GetRandom(0, CFootballField::s_CONST_FIELDWIDTH));
			for(int IndexB = 0; IndexB < Index; IndexB++ ) {
				if(IndexB == 0) {
					pKeeper = (CFootballKeeper*) m_pPlayer[IndexB];
					Found   = pKeeper->GetPosition() == Point;
				} else {
					pFielder = (CFootballOutfieldPlayer*) m_pPlayer[IndexB];
					Found    = pFielder->GetPosition() == Point;
				}
			}
			if(!Found) {
				for(int k = 0; k < CONST_MAX_PLAYER; k++ ) {
					if(k == 0) {
						pKeeper = (CFootballKeeper*) m_pPlayer[k];
						Found   = pKeeper->GetPosition() == Point;
					} else {
						pFielder = (CFootballOutfieldPlayer*) m_pPlayer[k];
						Found    = pFielder->GetPosition() == Point;
					}
				}
			}
		}while(Found);

		if(Index == 0)
			m_pPlayer[Index] = new CFootballKeeper(Point.GetX(), Point.GetY());
		else
			m_pPlayer[Index] = new CFootballOutfieldPlayer(Point.GetX(), Point.GetY());
	}
}

CFootballTeam::CFootballTeam(std::string _Name, CFootballTeam* _Team)
	: m_Score(0)
	, m_Name(_Name)
{
	//initialise players
	CPoint2D Point(0, 0);
	int Index = 0;
	bool Found = false;
	CFootballKeeper* pKeeper = NULL;
	CFootballOutfieldPlayer* pFielder = NULL;

	for(Index = 0; Index < CONST_MAX_PLAYER; Index++) {
		Found = false;
		do{
			Point.SetPoint(GetRandom(0, CFootballField::s_CONST_FIELDLENGTH), GetRandom(0, CFootballField::s_CONST_FIELDWIDTH));
			for(int IndexB = 0; IndexB < Index; IndexB++ ) {
				if(IndexB == 0) {
					pKeeper = (CFootballKeeper*) m_pPlayer[IndexB];
					Found  = pKeeper->GetPosition() == Point;
				} else {
					pFielder = (CFootballOutfieldPlayer*) m_pPlayer[IndexB];
					Found   = pFielder->GetPosition() == Point;
				}
			}
			if(!Found) {
				for(int k = 0; k < CONST_MAX_PLAYER; k++ ) {
					if(k == 0) {
						pKeeper = (CFootballKeeper*) _Team->m_pPlayer[k];
						Found  = pKeeper->GetPosition() == Point;
					} else {
						pFielder = (CFootballOutfieldPlayer*) _Team->m_pPlayer[k];
						Found   = pFielder->GetPosition() == Point;
					}
				}
			}
		}while(Found);

		if(Index == 0)
			m_pPlayer[Index] = new CFootballKeeper(Point.GetX(), Point.GetY());
		else
			m_pPlayer[Index] = new CFootballOutfieldPlayer(Point.GetX(), Point.GetY());
	}
}

CFootballTeam::~CFootballTeam(void)
{
	for(int Index = 0; Index < CONST_MAX_PLAYER; Index++) {
		m_pPlayer[Index]->~CFootballPlayer();
	}
}

CFootballKeeper* CFootballTeam::GetKeeper(void)
{
	return (CFootballKeeper *) m_pPlayer[0];
}

CFootballPlayer* CFootballTeam::GiveBallRandomPlayer()
{
	return m_pPlayer[GetRandom(0, CONST_MAX_PLAYER - 1)];
}

CFootballPlayer* CFootballTeam::GiveBallNextPlayer(CFootballPlayer* _Player)
{
	CFootballPlayer* pPlayer = NULL;
	CPoint2D PointStart = _Player->GetPosition();
	CPoint2D PointDest(0, 0);
	double MinDistance = 999999.0;
	double Distance = 0.0;

	for(int index = 0; index < CONST_MAX_PLAYER; index++) {
		PointDest = m_pPlayer[index]->GetPosition();
		Distance  = sqrt( pow( (float)(PointDest.GetX() - PointStart.GetX()), 2) - pow( (float)(PointDest.GetY() - PointStart.GetY()), 2));

		if(MinDistance > Distance && MinDistance > 0) {
			MinDistance = Distance;
			pPlayer = m_pPlayer[index];
		}
	}
	return pPlayer;
}

std::string CFootballTeam::GetName(void)
{
	return m_Name;
}

bool CFootballTeam::Intercept(void)
{
	// try to intercept
	if(GetRandom(1, 100) <= CFootballPlayer::s_CONST_CHANCE_TO_INTERCEPT) {
		return true;
		// not intercepted
	} else {
		return false;
	}
}

void CFootballTeam::IncrementScore()
{
	m_Score++;
}

int CFootballTeam::GetScore()
{
	return m_Score;
}